package com.bluej.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		for (Cookie ck : req.getCookies()) {
			System.out.println(ck.getName()+":"+ck.getValue());
			System.out.println(ck.getPath());
			System.out.println(ck.getDomain());
		}
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		System.out.println("--HelloServlet--");
		String owner = getServletConfig().getInitParameter("owner");
		String date = getServletConfig().getInitParameter("date");
		System.out.println("ServletConfig()[owner:" + owner + ";date:" + date
				+ "]");

		res.setContentType("text/html;charset=gb2312");// 避免中文乱码
		// 得到PrintWriter对象，
		PrintWriter out = res.getWriter();
		// 向客户端发送数据
		out.println("Hello World " + owner + "<br/>");
		// 关闭流
		out.close();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(req, resp);
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
	}

}
